/**
 * 
 */
package de.cbf.cam.dsa.model.entities.items.transfers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.TransferData;

import de.cbf.cam.dsa.model.entities.items.Item;


/**
 * @author Nebelritter
 *
 */
public class ItemTransfer extends ByteArrayTransfer implements ITransfer {
	private static final String TYPENAME = "itemTransfer";

	private static final int TYPEID = registerType(TYPENAME);

	private static ItemTransfer instance = new ItemTransfer();
	
	public static ItemTransfer getInstance() {
		return instance;
	}
	public void javaToNative(Object object, TransferData transferData) {
	      if (!checkItem(object) || !isSupportedType(transferData)) {
	        DND.error(DND.ERROR_INVALID_DATA);
	      }
	      Item item = (Item) object;
	      try {
	    	  // write data to a byte array and then ask super to convert to
	    	  // pMedium
	    	  ByteArrayOutputStream out = new ByteArrayOutputStream();
	    	  DataOutputStream writeOut = new DataOutputStream(out);
	    	 
	    	  writeItem(item, writeOut);	    	  
	        
	    	  byte[] buffer = out.toByteArray();	    	  
	    	  writeOut.close();
	    	  
	    	  super.javaToNative(buffer, transferData);
	      } catch (IOException e) {
	      }
	    }

    public Object nativeToJava(TransferData transferData) {
    	if (isSupportedType(transferData)) {
    		byte[] bufferAll = (byte[]) super.nativeToJava(transferData);
    		if (bufferAll == null){
    			return null;
    		}
    		Item item = new Item();
    		try {
    			ByteArrayInputStream in = new ByteArrayInputStream(bufferAll);
    			DataInputStream readIn = new DataInputStream(in);
    			while (readIn.available() > 20) {
    				
    				readItem(item, readIn);
    				
    			}
    		readIn.close();
    		return item;
	        } catch (IOException ex) {	        	
	          return null;
	        }        
      }
      return null;
    }
	
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.swt.dnd.Transfer#getTypeIds()
	 */
	@Override
	protected int[] getTypeIds() {
		return new int[] { TYPEID };
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.swt.dnd.Transfer#getTypeNames()
	 */
	@Override
	protected String[] getTypeNames() {
		return new String[] { TYPENAME };		
	}	

	boolean checkItem(Object object) {
		return (object != null && object instanceof Item);
	}

	protected boolean validate(Object object) {
		return checkItem(object);
	}
	
	
	
	static void writeItem(Item item,DataOutputStream writeOut) throws IOException{
		//guid
	  	TransferSubEntities.writeString(item.getGuid(),writeOut);
	  	//description
	  	TransferSubEntities.writeString(item.getDescription(),writeOut);
	  	  //name
	  	TransferSubEntities.writeString(item.getName(),writeOut);
	  	  //wear
	  	TransferSubEntities.writeString(item.getWear(),writeOut);
	  	  //weight
	  	writeOut.writeFloat(item.getWeight());	  	  
		
	}
	
	static void readItem(Item item,DataInputStream readIn) throws IOException{
		//string reading - GUID    		
		String guid = TransferSubEntities.readString(readIn);
		item.setGuid(guid);    				
		//string reading - Description
		String desc = TransferSubEntities.readString(readIn);
		item.setDescription(desc);   
		//string reading - Name    	
		String name = TransferSubEntities.readString(readIn);
		item.setName(name);    				
		//string reading - Wear
		String wear = TransferSubEntities.readString(readIn);
		item.setWear(wear);
		//float reading - weight
		item.setWeight(readIn.readFloat());
	}
	@Override
	public String toString() {
		return getClass().getSimpleName();
	}
	
}
